import const
from apscheduler.schedulers.blocking import BlockingScheduler
from utils import debug_util
from thread_pool import *
from utils import rabbitmq_util
from service import mongo_service

def get_threshold():
    '''
    获取阈值
    :return:
    '''
    result = mongo_service.get_threshold({'name': 'threshold'}, 'setting')
    const.THRESHOLD = result['threshold']
    debug_util.info('The threshold is ' + str(const.THRESHOLD))

def init_threshold():
    '''
    interval 触发器: （固定时间间隔触发）
    weeks(int):间隔几周
    days(int)：间隔几天
    hours(int)：间隔几小时
    minutes(int)：间隔几分钟
    seconds(int)：间隔几秒钟
    start_date(datetime或str)：开始时间
    end_date(datetime或str)：结束时间
    timezone(datetime.tzinfo或str):时区
    '''
    # 首次启动即开始第一次查询
    get_threshold()
    scheduler = BlockingScheduler()
    scheduler.add_job(get_threshold, 'interval', hours = 2)
    scheduler.start()

def startTask():
    MyThreadPool.threadPoolExecutor.submit(init_threshold)

def consuming():
    rabbitmq_util.start_consuming()

def start_consuming():
    debug_util.info('Start consuming for RabbitMQ')
    MyThreadPool.threadPoolExecutor.submit(consuming)
